using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Concurrent;
using System.Threading;

namespace Prologis.ShifterNT.Datasource
{


    public class StringSource : DataSourceSkeleton
    {

        System.Text.Encoding encoding = System.Text.Encoding.Default;
        public ConcurrentQueue<string> queue = new ConcurrentQueue<string>();


        public int Count { get { return queue.Count; } }

        string encodingString;
        public string Encoding
        {
            get { return encodingString; }
            set
            {
                encodingString = value;
                try
                {
                    encoding = System.Text.Encoding.GetEncoding(encodingString);
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("\r\n\r\nThe configured Encoding [{0}] is not a valid Encoding.\r\n\r\n ", encodingString), e);
                }
            }
        }


        bool acceptOnMatch = false;
        public bool AcceptOnMatch
        {
            get { return acceptOnMatch; }
            set { acceptOnMatch = value; }
        }


        Regex regex = null;
        string regexToMatch;
        public string RegexToMatch
        {
            get { return regexToMatch; }
            set
            {
                regexToMatch = value;
                regex = new Regex(regexToMatch, RegexOptions.Compiled);
            }
        }


        public void ApplyRecordString(string recordString)
        {
            queue.Enqueue(recordString);
        }



        public override MoveNextDecision GetNext(ref FieldList fieldList)
        {
            string line;

            while (true)
            {
                Thread.Sleep(1);

                if (true == queue.TryDequeue(out line))
                {

                    //mit Regex filtern
                    if (regex != null)
                    {
                        // Check the regex
                        if (regex.Match(line).Success)
                        {
                            if (!acceptOnMatch)
                            {
                                return MoveNextDecision.Filtered;
                            }
                        }
                        else
                        {
                            if (acceptOnMatch)
                            {
                                return MoveNextDecision.Filtered;
                            }
                        }
                    }

                    //mit Tokenizer in Felder zerlegen
                    fieldList = Tokenizer.Tokenize(ID, line);
                    if (!fieldList.HasValues)
                    {
                        return MoveNextDecision.Filtered;
                    }
                    else
                    {
                        return base.GetNext(ref fieldList);
                    }

                }
                else
                {
                    return MoveNextDecision.EOS;
                }

            }

        }

    }



}

